package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.DO.ClassChildrenDO;
import com.matt.foundation.DO.ClassChildrenListDO;
import com.matt.foundation.DO.ClassListDO;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.repository.base.ChildChildrenExtendRepository;
import com.matt.service.ChildrenService;
import com.matt.service.ClassesService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/8/10
 * *******************************
 */
@RestController
@RequestMapping("/pad/children")
@Api(tags = "学生/宝宝")
public class PadChildrenController {

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private ChildChildrenExtendRepository extendRepository;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private SsoCallService ssoCallService;

    @GetMapping("/media_statistics")
    @ApiOperation(value = "媒体数据分析，图片，视频，feed数量")
    public StatusResponse mediaStatistics(@RequestHeader(TICKET) String ticket, String childrenId){
        Map response = childrenService.getChildMediaStatisticsInfo(childrenId);
        return StatusResponse.success(response);
    }

    @GetMapping("/my_class_children")
    @ApiOperation(value = "我相关班级, 宝宝列表， 通常是给宝宝选择器使用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "formData", name = "withChildren", value = "是否同时查询孩子", required = true, dataType = "boolean"),
            @ApiImplicitParam(paramType = "formData", name = "classCategory", value = "1,托班， 2,早教, 3,全部， 默认全部", required = false, dataType = "integer")
    })
    public StatusResponse myChildren(@RequestHeader(TICKET) String ticket, boolean withChildren, Integer classCategory){
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String roleCode = PermissionConstant.Role.TEACHER.codeOf();
        if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_FULL})){
            roleCode = PermissionConstant.Role.GARDEN_LEADER.codeOf();
        }else if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_DOCTOR})){
            roleCode = PermissionConstant.Role.DOCTOR.codeOf();
        }

        if (classCategory == null || classCategory == 0){
            classCategory = 3;
        }

        List<ClassListDO> classesList = classesService.getClassesByStaffId(roleCode, staff.getId(), staff.getAreaId());

        if (classesList == null){
            return StatusResponse.success();
        }

        Map<String, ClassChildrenListDO> cache = new HashMap<>();

        Integer finalClassCategory = classCategory;
        List<ClassChildrenListDO> result = classesList.stream()
                .filter(o -> finalClassCategory == 3 || o.getCategory() == finalClassCategory)
                .map(o->{
            ClassChildrenListDO ret = ClassChildrenListDO.of(o);
            cache.put(o.getId(), ret);
            return ret;
        }).collect(Collectors.toList());

        if (withChildren) {
            List<String> classIds = classesList.stream().map(o -> o.getId()).collect(Collectors.toList());

            List<ClassChildrenDO> fullChildren = extendRepository.findChildrenClassesByStatus(staff.getAreaId(), classIds);
            if (fullChildren != null) {
                for (ClassChildrenDO cc : fullChildren) {
                    String classId = cc.getClassesId();

                    ClassChildrenListDO ccdo = cache.get(classId);
                    if (ccdo == null) {
                        continue;
                    }

                    if (ccdo.getChildrenList() == null) {
                        ccdo.setChildrenList(new ArrayList<>());
                    }

                    ccdo.getChildrenList().add(cc);
                }
            }
        }
        return StatusResponse.success(result);
    }
}
